package com.b.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.b.domain.Product;
import com.b.service.ProductService;
import com.b.utils.MinioUtils;
import com.b.vo.Result;

import jakarta.annotation.Resource;


/**
 * 商品表(product)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;
    @Autowired
    private MinioUtils minioUtils;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Product selectOne(Integer id) {
        return productService.selectByPrimaryKey(id);
    }

    //list(通过标题 分类id  描述模糊搜索分页)
    @GetMapping("list")
    public SearchPage<Product> list(
            @RequestParam(value = "title", defaultValue = "") String title,
            @RequestParam(value = "categoryId", defaultValue = "1" ) Integer categoryId,
            @RequestParam(value = "description", defaultValue = "") String description,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        SearchPage<Product> products = productService.findProductByTitleLikeAndCategoryIdAndDescriptionLike(title, categoryId, description, pageRequest);
        return products;
    }

    //增加修改
    @PostMapping(value = "save", consumes = "multipart/form-data")
    public Result save(
            @RequestPart("product") Product product,
            @RequestPart(value = "file", required = false) MultipartFile file
    ) {
        try {
            if (file != null && !file.isEmpty()) {
                String url = minioUtils.upload(file);
                product.setCoverImage(url);
            }
            if (product.getId() == null) {
                boolean b = productService.add(product);
                return b ? new Result().setCode("200").setMsg("添加成功") : new Result().setCode("400").setMsg("添加失败");
            } else {
                boolean f = productService.update(product);
                return f ? new Result().setCode("200").setMsg("修改成功") : new Result().setCode("400").setMsg("修改失败");
            }
        } catch (Exception e) {
            return new Result().setCode("500").setMsg("服务器异常：" + e.getMessage());
        }
    }

    //删除
    @PostMapping("delete/{id}")
    public Result delete( @PathVariable("id") Integer id) {
        try {
            boolean b = productService.deleteByPrimaryKey(id);
            if (b) {
                return new Result().setCode("200").setMsg("删除成功");
            } else {
                return new Result().setCode("400").setMsg("删除失败");
            }
        } catch (Exception e) {
            return new Result().setCode("500").setMsg("服务器异常：" + e.getMessage());
        }
    }
}
